package cn.com.cis.utils.tree;

import java.util.ArrayList;
import java.util.List;

public class TreeNode {

	private String nodeName;

	private TreeNode parentNode;
	private List<TreeNode> childrenNodes = new ArrayList<TreeNode>();

	private boolean isRoot;
	private boolean isLeaf;
	
	private boolean isChecked;

	public TreeNode getChildNodeByParentName(String childNodeName){
		for(TreeNode node : childrenNodes){
			if(null != node.getNodeName() && childNodeName.equals(node.getNodeName())){
				return node;
			}
		}
		return null;
	}

	public void addChildNode(String childNodeName) {
		
		boolean alreadyExists = false;
		for(TreeNode node : childrenNodes){
			if(null != node.getNodeName() && node.getNodeName().equals(childNodeName)){
				alreadyExists = true;
			}
		}
		
		if(!alreadyExists){
			TreeNode childNode = new TreeNode();
			childNode.setParentNode(this);
			childNode.setNodeName(childNodeName);
			childrenNodes.add(childNode);
		}
	}
	
	public void addChildNode(TreeNode childNode) {
		
		boolean alreadyExists = false;
		for(TreeNode node : childrenNodes){
			if(null != node.getNodeName() && node.getNodeName().equals(childNode.getNodeName())){
				alreadyExists = true;
			}
		}
		
		if(!alreadyExists){
			childNode.setParentNode(this);
			childrenNodes.add(childNode);
		}
	}
	
	public boolean containsLeafNode(){
		for(TreeNode child : childrenNodes){
			if(child.isLeaf){
				return true;
			}
		}
		
		return false;
	}

	public TreeNode getParentNode() {
		return parentNode;
	}

	public void setParentNode(TreeNode parentNode) {
		this.parentNode = parentNode;
	}

	public List<TreeNode> getChildrenNodes() {
		return childrenNodes;
	}

	public boolean isRoot() {
		return isRoot;
	}

	public void setRoot(boolean isRoot) {
		this.isRoot = isRoot;
	}

	public boolean isLeaf() {
		return isLeaf;
	}

	public void setLeaf(boolean isLeaf) {
		this.isLeaf = isLeaf;
	}
	
	public String getNodeName() {
		return nodeName;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}

	public boolean isChecked() {
		return isChecked;
	}

	public void setChecked(boolean isChecked) {
		this.isChecked = isChecked;
	}

}
